import CustomError from "../utils/CustomError";
module.exports = {

    /*
    * 小程序登录
    * */
    wxLogin: function () {
        return new Promise(function (resolve, reject) {
            wx.login({
                success (res) {
                    if (res.code) {
                        resolve(res.code)
                    } else {
                        reject(res)
                    }
                },
                fail: function (err) {
                    reject(err)
                }
            })
        })
    },

    /*
    * 模态框
    * */
    showModal: function (data = {}) {
        return new Promise(function (resolve, reject) {
            wx.showModal({
                ...data,
                success: function (res) {
                    resolve(res)
                },
                fail: function (err) {
                    reject (new CustomError (err, "Wx.showModal").parse())
                }
            })
        })
    },

    /*
    * 下载
    * */
    downloadFile: function (data = {}) {
        return new Promise(function (resolve, reject) {
            wx.downloadFile({
                ...data,
                success: function (res) {
                    if (res.statusCode === 200) {
                        resolve(res)
                    } else {
                        reject (new CustomError ("下载文件失败", "Wx.downloadFile").parse())
                    }
                },
                fail: function (err) {
                    reject (new CustomError (err, "Wx.downloadFile").parse())
                }
            })
        })
    },

    /*
    * 查看文档
    * */
    openDocument: function (data = {}) {
        return new Promise(function (resolve, reject) {
            wx.openDocument({
                ...data,
                success: function (res) {
                    resolve(res)
                },
                fail: function (err) {
                    reject (new CustomError (err, "Wx.openDocument").parse())
                }
            })
        })
    }
}